﻿var vanim = 0;
var HsCms = {
	//确认框
	Confirm: function (msg, callback, cancelback, closeback) {
		layui.use(['layer'], function () {
			var layer = layui.layer;
			layer.confirm(msg, {
				title: '系统提示', zindex: 100, cancel: function (index, layero) {
					if (closeback)
						closeback(index);
				}
			}, function () {
				if (callback) {
					callback(100);
				}
			}, function () {
				if (cancelback) {
					cancelback(100);
				}
			});
		});
	},
	//提示框
	Alert: function (msg, callback) {
		layui.use(['layer'], function () {
			var layer = layui.layer;
			layer.alert(msg, { title: '系统提示', zindex: 999, }, function (index) {
				if (callback)
					callback(index);
				layer.close(index);
			});
		});
	},
	//加载进度条
	Load: function (type) {
		layui.use(['layer'], function () {
			var layer = layui.layer;
			layer.load(type);
		});
	},
	//信息提示
	Msg: function (msg, callback) {
		layui.use(['layer'], function () {
			var layer = layui.layer;
			layer.msg(msg, { time: 800, zIndex: 9999 });

			if (callback) {
				callback();
			}
		});
	},
	//Ajax 同步Post
	AjaxPost: function (url, data, okcallback, beforecallback, errorcallback) {
		layui.use(['layer'], function () {
			var layer = layui.layer;
			$.ajax({
				url: url,
				type: "POST",
				async: false,
				data: data,
				success: function (res) {
					layer.closeAll('loading');
					if (okcallback) {
						okcallback(res);
					}
				},
				beforeSend: function () {
					layer.load(2);
					if (beforecallback) {
						beforecallback();
					}
				},
				error: function (e) {
					layer.closeAll('loading');
					if (errorcallback) {
						errorcallback(e);
					} else {
						HsCms.Msg("系统繁忙，请稍候再试~");
					}
				}
			});
		});
	},
	AjaxJson: function (url, type, data, okcallback, beforecallback, errorcallback) {
		$.ajax({
			url: url,
			type: type,
			dataType: "json",
			async: false,
			data: data,
			beforeSend: function () { if (beforecallback) { beforecallback(); } },
			success: function (res) { if (okcallback) okcallback(res); },
			error: function (e) {
				if (errorcallback) { errorcallback(); } else { HsCms.Msg("系统繁忙，请稍候再试~"); }
			}
		});
	},
	//子页面关闭时刷新父页面table
	ParentLoad: function (tableId) {
		parent.layui.table.reload(tableId);
	},
	//子页面关闭时再父页面提示信息
	ParentMsg: function (msg) {
		parent.layer.msg(msg);
	},
	//关闭层(弹出窗是新的页面的时候)
	CloseFrame: function () {
		var index = parent.layer.getFrameIndex(window.name);
		parent.layer.close(index);
	},
	ToDate: function (datet) {
    var d = new Date(datet);
    var curr_date = d.getDate();
    var curr_month = d.getMonth() + 1; 
    var curr_year = d.getFullYear();
    String(curr_month).length < 2 ? (curr_month = "0" + curr_month): curr_month;
    String(curr_date).length < 2 ? (curr_date = "0" + curr_date): curr_date;
    var yyyyMMdd = curr_year + "-" + curr_month +"-"+ curr_date;
    return yyyyMMdd;
    },

	//弹窗(两者选择其一)
	OpenPart: function (type, id, url, title, wh, btn, callback, callbackYes) {
		layer.open({
			type: type
			, title: title //不显示标题栏
			, area: wh
			, id: id //设定一个id，防止重复弹出,保证唯一
			, btn: btn
			, btnAlign: 'r'
			, moveType: 1 //拖拽模式，0或者1
			, content: url
			, anim: vanim
			, success: function (layero, index) {
				if (callback)
					callback(layero, index);
			},
			yes: function (index, layero) {
				if (callbackYes) {
					callbackYes(index, layero);
				}
				//layer.close(index); //如果设定了yes回调，需进行手工关闭
			}
		});
	}

}